package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     *
     * @param setmealDto
     */
    @Override
    public void saveWithDish(SetmealDTO setmealDto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto, setmeal);

        setmealMapper.insert(setmeal);

        long setmealId = setmeal.getId();
        List<SetmealDish> dishes = setmealDto.getSetmealDishes();
        for (SetmealDish dish : dishes) {
            dish.setSetmealId(setmealId);
        }
        setmealDishMapper.insertBeatch(dishes);
    }

    /**
     * 套餐的分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        ;
        Page<SetmealVO> page = setmealMapper.pageQuary(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id删除菜品，停售的不能删
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deteleByIds(List<Long> ids) {
        for (Long id : ids) {
            Setmeal s = setmealMapper.selectStatusById(id);
            if (s.getStatus() == StatusConstant.ENABLE)
                throw new DeletionNotAllowedException("套餐正在售卖中，不能删除");
        }

        setmealMapper.deleteByIds(ids);
        setmealDishMapper.deleteByIds(ids);
    }

    /**
     * 根据id 查询套餐的详细信息
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public SetmealVO getSetmealVoById(long id) {
        Setmeal smeal = setmealMapper.selectById(id);
        // 根据套餐id 查询套餐 菜品信息
        List<SetmealDish> sd = setmealDishMapper.selectBySetmealId(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(smeal, setmealVO);
        setmealVO.setSetmealDishes(sd);
        return setmealVO;
    }

    /**
     * 修改套餐信息以及与套餐相关的菜品信息
     *
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateWithDish(SetmealDTO setmealDto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto, setmeal);
        setmealMapper.update(setmeal);

        ArrayList<Long> longs = new ArrayList<>();
        longs.add(setmealDto.getId());
        setmealDishMapper.deleteByIds(longs);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishMapper.insertBeatch(setmealDishes);
    }

    /**
     * 修改菜品起售与停售
     *
     * @param status
     * @param id
     */
    @Transactional
    @Override
    public void startOrStop(Integer status, Long id) {
        // 如果要起售的套餐中包含停售的餐品则不能起售
        if (status == StatusConstant.ENABLE) {
            List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);
            for (SetmealDish setmealDish : setmealDishes) {
                Dish item = dishMapper.getBYId(setmealDish.getDishId());
                if (item.getStatus() == StatusConstant.DISABLE && status == StatusConstant.ENABLE)
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        Setmeal st = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.updataStatusById(st);

    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }


}
